<?php

namespace backend\controllers;

use Yii;
use backend\models\Show;
use backend\models\ShowSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\Image;
use PHPThumb\GD;
use liangzoe\mobilesmser\TextSmser;
use yii\web\UploadedFile;
use backend\models\PrfConfig;
use yii\web\ForbiddenHttpException;
use backend\models\SysHomeBanner;
use backend\models\ShowPos;
use backend\models\GrpColor;
use backend\models\ShowColor;
use backend\models\ShowStyle;
use backend\models\SysImage;

/**
 * ShowController implements the CRUD actions for Show model.
 */
class ShowController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    public function beforeAction($action)
    {
    	//如果未登录，则直接跳转
    	if(Yii::$app->user->isGuest){
    		return $this->redirect(['/site/login']);
    	}
    
    
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	if(\Yii::$app->user->can('shows-manage')){
    		return true;
    	}else{
    		throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
    	}
    }
    /**
     * Lists all Show models.
     * @return mixed
     */
    
    public function actionIndex()
    {
        $searchModel = new ShowSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * 套装管理 点击秀家 显示秀家列表
     * @return string
     */
    public function actionImgIndex(){
        $searchModel=new ShowSearch();
        $dataProvide=$searchModel->img_search(Yii::$app->request->queryParams);
        return $this->render('img_index',['searchModel'=>$searchModel,'dataProvider'=>$dataProvide]);
    }

    /**
     * Displays a single Show model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
    	$model = $this->findModel($id);
    	
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    /**
     * flash修改的秀家管理
     */
    public function actionImgUpdate($id){
        
        $model = $this->findModel($id);
        empty($model->top_time)?$model->top_time_1='0':$model->top_time_1='1';
        
        //显示已选中的checkbox
        $color = ShowColor::find()->where(['show_id' =>$id])->asArray()->all();
        $style = ShowStyle::find()->where(['show_id' =>$id])->asArray()->all();
         
        $model->color_list = $this->get_ext($color, 'color_id');
        $model->style_list = $this->get_ext($style, 'style_id');
        
        
        if ($model->load(Yii::$app->request->post())) {
            
            //操作系列  色系
            
            
            if($model->top_time_1 === '1'){$model->top_time = time();}//置顶
            if($model->top_time_1 === '0'){$model->top_time = 0;}//不置顶
            $result=$model->save();
            //修改时先删除show_id的数据
            ShowColor::deleteAll(['show_id' => $model->id]);
            ShowStyle::deleteAll(['show_id' => $model->id]);
            
            $this->add_ext($model->id, $model->color_list, $model->style_list);
            
            
            if($result) return $this->redirect(['img-update', 'id' => $model->id]);
        } else {
            return $this->render('img_update', [
                'model' => $model,
            ]);
        }
        
    }   
 
    /**
     * flash 添加秀家
     */
    public function actionImgCreate(){
        $model =new Show();
        empty($model->top_time)?$model->top_time_1='0':$model->top_time_1='1';
        //显示已选中的checkbox
      /*   $color = ShowColor::find()->where(['show_id' =>$id])->asArray()->all();
        $style = ShowStyle::find()->where(['show_id' =>$id])->asArray()->all();
        
        $model->color_list = $this->get_ext($color, 'color_id');
        $model->style_list = $this->get_ext($style, 'style_id'); */
        
        
        if ($model->load(Yii::$app->request->post())) {
            
            //操作系列  色系
            if($model->top_time_1 === '1'){$model->top_time = time();}//置顶
            if($model->top_time_1 === '0'){$model->top_time = 0;}//不置顶
            $result=$model->save();
            $this->add_ext($model->id, $model->color_list, $model->style_list);
            if($result) return $this->redirect(['img-index']);
        } else {
            return $this->render('img_create', [
                'model' => $model,
            ]);
        }
    }
    /**
     * Creates a new Show model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Show();

        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
        	
        	if($model->top_time_1 === '1'){$model->top_time = time();}//置顶
        	if($model->top_time_1 === '0'){$model->top_time = 0;}//不置顶
        	
        	$model->publish_uid = Yii::$app->user->identity->id;
        	$model->publish_time = time();

        	
        	$model->image_upload = UploadedFile::getInstance($model, 'image_upload');
        	if ($model->image_upload) {
        		if($model->state == '1') $file_menu = '492';
        		if($model->state == '2') $file_menu = '493';
        		if($model->state == '3') $file_menu = '494';
        		if($model->state == '4') $file_menu = '495';
        		
        		$arr = Image::save_image_to_files($model, $file_menu, $model->name, 'show_image_thumb', 'show_image_small');
        		$model->img_small_id = $arr['img_small_id'];
        		$model->img_thumb_id = $arr['img_thumb_id'];
        		$model->img_url_id = $arr['img_url_id'];
        		
        		$model->image_upload = '';
        	}
        	
            if($model->save())return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Show model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
		empty($model->top_time)?$model->top_time_1='0':$model->top_time_1='1';
        if ($model->load(Yii::$app->request->post())) {
        	if($model->top_time_1 === '1'){$model->top_time = time();}//置顶
        	if($model->top_time_1 === '0'){$model->top_time = 0;}//不置顶
        	
        	$model->image_upload = UploadedFile::getInstance($model, 'image_upload');
        	if ($model->image_upload) {
        		if($model->state == '1') $file_menu = '492';
        		if($model->state == '2') $file_menu = '493';
        		if($model->state == '3') $file_menu = '494';
        		if($model->state == '4') $file_menu = '495';
        	
        		$arr = Image::save_image_to_files($model, $file_menu, $model->name, 'show_image_thumb', 'show_image_small',$model->img_url_id,$model->img_thumb_id,$model->img_small_id);
        		$model->img_small_id = $arr['img_small_id'];
        		$model->img_thumb_id = $arr['img_thumb_id'];
        		$model->img_url_id = $arr['img_url_id'];
        		$model->image_upload = '';
        	}
        	
           if($model->save()) return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Show model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }
    /**
     * 删除秀家
     */
    public function actionImgDelete($id){
        $this->findModel($id)->delete();
        return $this->redirect(['img-index']);
        
    }
    /**
     * Finds the Show model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Show the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Show::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
   
    
    public function actionToBanner($id)
    {
    	$this->layout = 'main_small';
    	/*检查是否已存在*/
    	$banner_if_exist = SysHomeBanner::findOne(['action_id'=>$id,'action'=>'show']);
    	if($banner_if_exist != null){
    		
    		//return $this->redirect(Yii::$app->request->referrer);
    		return $this->render('to_banner',[
    				'info' =>'选择的秀家，已加入banner，不能重复添加',
    				]);
    		 
    	}
    	$model_banner = new SysHomeBanner();
    	$model_show = $this->findModel($id);
    	$model_banner->type = 2;
    	$model_banner->name= $model_show->name;
    	$model_banner->designer_id = $model_show->publish_uid;
    	$model_banner->img_small_id =$model_show->img_small_id;
    	$model_banner->img_thumb_id = $model_show->img_thumb_id;
    	$model_banner->img_url_id = $model_show->img_url_id;
    	$model_banner->create_time = time();
    	$model_banner->top_time = time();
    	$model_banner->useing = 1 ;
    	$model_banner->action = 'show';
    	$model_banner->action_id = $id;
    	if($model_banner->save()){
    		return $this->render('to_banner',[
    				'info' =>'成功添加到推荐',
    				]);
    	}
    }
 
    /**
     * 推荐到首页轮播图或者首页推荐 根据传递的type来判断type类型
     * @param 秀家 $id
     * @param   $type 1 banner   2 首页推荐
     */
    public function actionToBannerByType($id,$type){
        $this->layout = 'main_small';
        /*检查是否已存在*/
        $banner_if_exist = SysHomeBanner::findOne(['action_id'=>$id,'action'=>'show','type'=>$type]);
        if($banner_if_exist != null){
            
            //return $this->redirect(Yii::$app->request->referrer);
            return $this->render('to_banner',[
                'info' =>'选择的秀家，已加入推荐，不能重复添加',
            ]);
            
        }
        $model_banner = new SysHomeBanner();
        $model_show = $this->findModel($id);
        $model_banner->type = $type;
        $model_banner->name= $model_show->name;
        $model_banner->designer_id = $model_show->publish_uid;
        $model_banner->img_small_id =$model_show->img_small_id;
        $model_banner->img_thumb_id = $model_show->img_thumb_id;
        $model_banner->img_url_id = $model_show->img_url_id;
        $model_banner->create_time = time();
        $model_banner->top_time = time();
        $model_banner->useing = 1 ;
        $model_banner->action = 'show';
        $model_banner->action_id = $id;
        if($model_banner->save()){

            return $this->render('to_banner',[
                'info' =>'成功添加到推荐',
            ]);
        }
        
    }
    
    /**
     * 从数据库中查出数据并转换为数组
     * @param    $item_id
     * @param   $model
     * @param   $field
     * @return multitype:
     */
    public function get_ext($model,$field)
    {
        $arr = array();
        foreach ($model as $value){
            array_push($arr, $value[$field]);
        }
        return $arr;
    }
    /**
     * 添加色系  系列
     * @param unknown $show_id
     * @param unknown $color_list
     * @param unknown $style_list
     */
    public function add_ext($show_id,$color_list,$style_list)
    {
        if(!empty($color_list)){
            foreach ($color_list as $value)
            {
                $model_color = new ShowColor();
                $model_color->show_id = $show_id;
                $model_color->color_id = $value;
                $model_color->save();
            }
        }
        if(!empty($style_list)){
            foreach ($style_list as $value)
            {
                $model_style = new ShowStyle();
                $model_style->show_id = $show_id;
                $model_style->style_id = $value;
                $model_style->save();
            }
        }
    }
    /**
     * 修改秀家的底图
     * @param   $id
     */ 
    public function actionUpdateImage($id,$show_id){
        if($id==0||$show_id==0){
            echo 2;exit();
        }else{
            $show=Show::findOne($show_id);
            if($show){
                $show->img_url_id=$id;
                $show->img_small_id=$id;
                $show->img_thumb_id=$id;
                $show->publish_uid=intval(Yii::$app->user->identity->id);
                $show->publish_time=time();
                $result=$show->save();
                if($result){
                    echo 1;exit();
                }else{
                    echo 0;exit();
                }
            }else{
                echo 2;exit();
            }
            
        }
        
    }
    /**
     * 查看单品
     * @param  秀家的id   $show_id
     */
    public function actionShowPos($show_id){
        $this->layout = 'main_none';
       $show=Show::findOne($show_id);
       $pos_model=ShowPos::find()->where("sid=$show_id")->all();
       //获取秀家图片的宽高
       $img_id=intval($show->img_url_id);
       if($img_id>0){
           $img=SysImage::findOne($img_id);
           
           return $this->render('pos',['model'=>$show,'pos_model'=>$pos_model,'img'=>$img]);
       }else{
          return $this->redirect(['show/img-index']);
       }
    }
    /**
     * 修改单品显示地图的xy坐标 
     */
    public function actionUpPos($id,$x,$y){
        $pos=ShowPos::findOne($id);
        $sid=$pos->sid;
        $show=Show::findOne($sid);
        $img=SysImage::findOne($show->img_thumb_id);
        $rang=920/($img->width);
        if($pos){
           /*  $pos->posx=$x+30;
            $pos->posy=$y+30; */
            $pos->posx=intval($x/$rang+15);
            $pos->posy=intval($y/$rang+20);
            $result=$pos->save();
            echo $result;exit();
        }else{
            echo 2;exit();
        }
        
        
    }
    /**
     * 修改单品
     * @param   $id
     */
    public function actionItemIndex($id){
        $pos=ShowPos::find()->where("sid=$id");
        $dataProvider = new ActiveDataProvider([
            'query' => $pos 
        ]);
        return $this->render('item_index',['dataProvider'=>$dataProvide]);
    }

    /**
     * 修改pos数据
     */
    public function actionPosUpdate($id,$action_id,$pos_type){
     $pos=ShowPos::findOne($id);
     $action='';
     if($pos){
         $action='';
         switch ($pos_type){
             case 1:
                 $action='url';
                 break;
             case 2:
                 $action='set';
                 break;
             case 3:
                 $action='item';
                 break;
         }

         if($action=="url"){
             $pos->url=$action_id;
             $pos->action_id=0;


         }else{
             $pos->action_id=$action_id;
         }
         $pos->action=$action;

         $result=$pos->save();

         echo $result;exit();
     }else{
         echo 2;exit();
     }

    }

    /**
     * 添加pos
     */
    public function  actionAddPos($sid){
       if(intval($sid)>0){
           $pos=new ShowPos();
           $pos->sid=$sid;
           $pos->posx=50;
           $pos->posy=50;
           $pos->action="item";
           $result=$pos->save();
           echo $result;exit();
       }else{
           echo 2;exit();
       }
    }

    /**
     * @param $id
     * @throws \yii\db\StaleObjectException删除pos
     */
    public function actionDelPos($id){

        if(intval($id)>0){
            $pos=ShowPos::findOne($id);
            $result=$pos->delete();
            echo $result;exit();
        }else{
            echo 2;exit();
        }
    }
}
